//
//  ZGDetailAddressController.m
//  NewBee
//
//  Created by leergou on 16/8/21.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "ZGDetailAddressController.h"
#import "ZGSendAddressModel.h"
#import "ZGAdressInfoView.h"

#define kMargin 5

@interface ZGDetailAddressController ()<UIGestureRecognizerDelegate>

/* scrollView */
@property (nonatomic,strong) UIScrollView *scrollView;

/* dettailInfoView */
@property (nonatomic,strong) ZGAdressInfoView *infoView;

/* deleteLabel */
@property (nonatomic,strong) UILabel *deleteLabel;

/* deleteButton */
@property (nonatomic, strong) UIButton *deleteButton;

/* 城市数组 */
@property (nonatomic,strong) NSArray *cityList;

@end

@implementation ZGDetailAddressController


#pragma mark - 视图生命周期 -
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupNav];
}

#pragma mark - 点击事件 -
- (void)backButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveButtonClick{
    
    if (!self.addressModel) {
        self.addressModel = [[ZGSendAddressModel alloc] init];
    }
    
    // 赋值 model
    self.addressModel.name = self.infoView.nameTextField.text;
    self.addressModel.gender = self.infoView.currentButton.currentTitle;
    self.addressModel.telNum = self.infoView.telNumTextField.text;
    self.addressModel.city = self.infoView.cityTextField.text;
    self.addressModel.zone = self.infoView.zoneTextField.text;
    self.addressModel.detailAddr = self.infoView.detailTextField.text;

    // 将模型传出去
    if (self.operationType == OperationTypeAdd) {
        // 通知上一个页面刷新
        if (self.callBack) {
            self.callBack(OperationTypeAdd);
        }
    } else if (self.operationType == OperationTypeEdite){
        // 通知上一个页面刷新
        if (self.callBack) {
            self.callBack(OperationTypeEdite);
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)deleteAddr:(UITapGestureRecognizer *)tap{
    NSLog(@"删除当前地址");
    if (self.callBack) {
        self.callBack(OperationTypeDelete);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - setupUI -
- (void)setupUI {
    
    self.view.backgroundColor = kGlobalGray;
    
    if (self.operationType == OperationTypeAdd) {
        [self.deleteButton setTitle:@"请输入完整的个人信息" forState:UIControlStateNormal];
    }
    
    // 1.添加子控件
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.infoView];
    [self.scrollView addSubview:self.deleteButton];

    // 2.添加约束
    [self.infoView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kMargin);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(240);
    }];
    
    [self.deleteButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.infoView.bottom).offset(kMargin);
        make.width.equalTo(SCREEN_WIDTH);
        make.height.equalTo(50);
    }];
    
    // 3.子控件赋值
    self.infoView.addressModel = self.addressModel;
}

#pragma mark - 设置导航 -
- (void)setupNav{
    
    if (!self.addressModel) {
        self.title = @"增加地址";
    } else {
         self.title = @"修改地址";
    }
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"v2_goback"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    rightButton.frame = CGRectMake(0, 0, 50, 40);
    [rightButton addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

#pragma mark - lazy -
- (UIButton *)deleteButton{
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteButton.backgroundColor = [UIColor whiteColor];
        [_deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_deleteButton setTitle:@"删除当前地址" forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteAddr:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

- (NSArray *)cityList{
    if (!_cityList) {
        _cityList = @[@"北京市", @"上海市", @"天津市", @"广州市", @"佛山市",@"深圳市", @"廊坊市", @"武汉市", @"苏州市", @"无锡市"];
    }
    return _cityList;
}

- (UILabel *)deleteLabel{
    if (!_deleteLabel) {
        _deleteLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _deleteLabel.text = @"删除当前地址";
        _deleteLabel.backgroundColor = [UIColor yellowColor];
        _deleteLabel.textAlignment = NSTextAlignmentCenter;
        
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteAddr:)];
        tap.delegate = self;
        [_deleteLabel addGestureRecognizer:tap];
    }
    return _deleteLabel;
}

- (ZGAdressInfoView *)infoView{
    if (!_infoView) {
        _infoView = [[ZGAdressInfoView alloc] initWithFrame:CGRectZero];
        _infoView.cityList = self.cityList;
        __weak typeof(self) weakSelf = self;
        _infoView.genderSelect = ^(NSString *gender){
            weakSelf.addressModel.gender = gender;
        };
    }
    return _infoView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        
        _scrollView.delaysContentTouches = NO;
        _scrollView.canCancelContentTouches = NO;
        
        _scrollView.scrollEnabled = YES;
        _scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT * 1.2);
        _scrollView.bounces = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}


@end















